var path = window.location.href;
var param = path.split("?")
var a=param[1].split("&")
var i=a[0].split("=")
var id=i[1].split(",")
var tit=a[1].split("=")
var title=tit[1].split(",")

var open=a[2].split("=")
var openTime=open[1].split(",")

var end=a[3].split("=")
var endTime=end[1].split(",")

var sp=a[4].split("=")
var sequence=sp[1].split(",")

var cate=a[5].split("=")
var category_id=cate[1].split(",")

var image_url=decodeURIComponent(decodeURIComponent(decodeURIComponent(getQueryString("image_url"))))
var image_path=decodeURIComponent(decodeURIComponent(decodeURIComponent(getQueryString("image_path"))))
var color_value=decodeURIComponent(decodeURIComponent(decodeURIComponent(getQueryString("color_value"))))
var CangName=""
var code=""
var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
$.each(warehouses.data,function(k,s){
    code +=s.warehouseCode+ ","
    CangName+=s.warehouseName+"/"

})
//去掉最后一个逗号(如果不需要去掉，就不用写)
if (code.length > 0) {
    code = code.substr(0, code.length - 1);
}
if (CangName.length > 0) {
    CangName = CangName.substr(0, CangName.length - 1);
}
var r=null
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.search_resource_conf,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            resource_id:getQueryString("id")
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                code=data.data[0].warehouse_code;
                code=code.split(',')
                var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
                for(var i=0;i<code.length;i++){//id相同为选中状态
                    console.log(code[i])
                    if(code.indexOf(code[i])>= 0){
                        $.each(warehouses.data,function(k,s){
                            if(code[i]==s.warehouseCode){
                                var warecode= document.getElementById('ying'+s.warehouseCode+'')
                                warecode.checked=true
                            }

                        })
                    }
                }
                code=data.data[0].warehouse_code;
                CangName=data.data[0].warehouse_name;
            }
        }
    })
})
function changeCang() {
    code=''
    CangName=''
    var warcode=""//id
    var warcodename=""//name
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
        if(document.getElementById('ying'+s.warehouseCode+'').checked==true){//获取选中的id
            warcode+=document.getElementById('ying'+s.warehouseCode+'').value+ ","//逗号拼接获取到的所以id
            warcodename+=document.getElementById('ying'+s.warehouseCode+'').name+ "/"
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    if (warcodename.length > 0) {
        warcodename = warcodename.substr(0, warcodename.length - 1);
    }
    code=warcode
    CangName=warcodename
}
$(function () {
    $("#biaoti").val(decodeURI(title))
    $("#url").val(image_url)
    $("#logmin").val(decodeURI(openTime))
    $("#logmax").val(decodeURI(endTime))
    $("#lunbo").val(sequence)
    $("#myImg").attr("src",image_path)
    $("#down_color").val(color_value)

})
function changImg(e){
    var image = new Image();
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            layer.msg('请上传图片!', {icon: 6, time: 2000});
            $("#file").val("")
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function(e) {
            image.src =e.target.result ;
            image.onload = function() {
                if(file.size>5*1024*1024){
                    layer.msg('图片不得大于5M!', {icon: 6, time: 2000});
                }else{
                    if (this.width!=1000||this.height!=450){
                        layer.msg('图片大小限制为1000*450!', {icon: 6, time: 2000});
                        r=1
                        $("#file").val("")
                        return;
                    } else {
                        $("#myImg").css({width:this.width/5+"px",height:this.height/5+"px",cursor:"pointer"});
                        $("#myImg").attr("src",e.target.result);
                    }
                }
            };
        }
    }
}
$("#form-member").validate({
    rules: {
        title:"titleCheck",
        lunbuto:"sequenceCheck"
    },
    //提交表单数据
    submitHandler:function(form){
        //循环class，获取到每个check的选中状态
        if (code==''){
            var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

            $.each(warehouses.data,function(k,s){
                var warecode= document.getElementById('ying'+s.warehouseCode+'')
                $(warecode).css('outline','1px solid red')

            })
            layer.msg("请选择配置平台/地域？", {icon: 6, time: 3000});
            return false
        }
        var formData = new FormData();
        formData.append("started_at",$("#logmin").val());
        formData.append("resource_id",getQueryString("id"));
        formData.append("end_in",$("#logmax").val());
        formData.append("image_url",$("#url").val());
        formData.append("title",$("#biaoti").val());
        if (r==null){
            formData.append("file",$("#file")[0].files[0]);
        }
        formData.append("sequence",$("#lunbo").val());
        var quzhi=JSON.parse(web.readCookie("loginData"))
        formData.append("last_modified_name",quzhi.user.username);
        formData.append("last_modified_by",quzhi.user.user_ID);
        /*平台和仓库*/
        formData.append("warehouse_code",code);
        formData.append("warehouse_name",CangName);
        formData.append("color_value",$("#down_color").val());
        console.log($("#file")[0].files[0])
        $.ajax({
            url: root.sroreinter.save_resource_conf,
            cache: false,  //禁用缓存
            type: "POST",
            // 告诉jQuery不要去处理发送的数据
            processData : false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType : false,
            data: formData,
            success: function (data) {
                console.log(data)
                if (data.errorCode=="92"){
                    layer.msg('您添加的开始时间小于已存在数据的结束时间!', {icon: 6, time: 3000});
                }
                if (data.errorCode=="94"){
                    layer.msg(data.errorMessage, {icon: 6, time: 3000});
                }
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    parent.isEdit = true;
                    parent.queryDataFn();
                    setTimeout( function () {
                        cancelLayerNoRefreshFn();
                    },700);
                }
                if (data.errorCode=="93"){
                    layer.msg('开始时间不能大于结束时间,请重新查看', {icon: 6, time: 3000});
                    return
                }
            }
        });
    }
})
//初始化客户归属下拉列表
function ascription() {
    var user_data = {}
    var succCallBack = function (data, status, response) {
        //console.log(data)
        if (data.errorCode == "0") {
            var dataInter = data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}
function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
$('#pic').click(function (event) {
    event.preventDefault()
    $('#file').click()
})